package com.sphd.education.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sphd.education.R;
import com.sphd.education.activitys.LessonMyEvaluationActivity;
import com.sphd.education.info.LessonInfo;
import com.sphd.education.utils.ToastUtils;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by admin on 2015/10/27.
 */
public class HistoryAdapter extends BaseAdapter implements View.OnClickListener {
    Context context;
    List<LessonInfo> lessonInfoList;

    public HistoryAdapter(Context context, List<LessonInfo> lessonInfoList) {
        this.context = context;
        this.lessonInfoList = lessonInfoList;
    }

    @Override
    public int getCount() {
        return lessonInfoList.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_lessonhistory_item, null);
            viewHolder = new ViewHolder(convertView);
            viewHolder.lessonSchoolName.setText(lessonInfoList.get(position).getSchoolName().toString());
            viewHolder.lessonLessonName.setText(lessonInfoList.get(position).getName().toString());
            viewHolder.lessonClassName.setText(lessonInfoList.get(position).getBanji() + "");
            viewHolder.historyBeginTime.setText(lessonInfoList.get(position).getBeginTime());
            viewHolder.historyEndTime.setText(lessonInfoList.get(position).getEndTime());
            viewHolder.hostoryEvaluation.setText(lessonInfoList.get(position).getMyEvalution());

            viewHolder.historyBack.setText(lessonInfoList.get(position).getMyBack().toString());
            if (lessonInfoList.get(position).getAnonymousType().equals("0")||lessonInfoList.get(position).getAnonymousType().equals("")){
                viewHolder.imageNi.setVisibility(View.GONE);
            }

            viewHolder.lessonMyEvaluation.setOnClickListener(this);
            viewHolder.lessonMyEvaluation.setTag(position);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (lessonInfoList.get(position).getMyBack().isEmpty() && lessonInfoList.get(position).getMyEvalution().isEmpty()) {
            viewHolder.myEvalution.setVisibility(View.GONE);
        } else {
            viewHolder.lessonMyEvaluation.setVisibility(View.GONE);
            if (lessonInfoList.get(position).getMyEvalution().isEmpty()) {
                viewHolder.hostoryEvaluation.setVisibility(View.GONE);
            }
        }

        return convertView;
    }

    @Override
    public void onClick(View v) {
        int a = (int) v.getTag();
        ToastUtils.MyToast(context, a + "");
        Intent intent = new Intent(context, LessonMyEvaluationActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("SchoolName", lessonInfoList.get(a).getSchoolName());
        bundle.putString("Name", lessonInfoList.get(a).getName());
        bundle.putString("Banji", lessonInfoList.get(a).getBanji() + "");
        bundle.putString("LessonTime", "");
        bundle.putString("LessonPart", "");
        bundle.putString("State", "");
////        bundle.putString();
        intent.putExtras(bundle);
        context.startActivity(intent);

    }

    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'layout_lessonhistory_item.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder {
        @Bind(R.id.lesson_school_name)
        TextView lessonSchoolName;
        @Bind(R.id.lesson_my_evaluation)
        TextView lessonMyEvaluation;
        @Bind(R.id.lesson_lesson_name)
        TextView lessonLessonName;
        @Bind(R.id.lesson_class_name)
        TextView lessonClassName;
        @Bind(R.id.history_begin_time)
        TextView historyBeginTime;
        @Bind(R.id.history_end_time)
        TextView historyEndTime;
        @Bind(R.id.hostory_evaluation)
        TextView hostoryEvaluation;
        @Bind(R.id.history_back)
        TextView historyBack;
        @Bind(R.id.my_evalution)
        LinearLayout myEvalution;
        @Bind(R.id.image_ni)
        ImageView imageNi;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
